import {Component, OnInit} from "@angular/core";
import {PageSubject} from "../../../util/page-subject";
import {OneDialogService} from "../../../widgets/one-dialog/one-dialog.service";
import {Dialog} from "../../../widgets/one-dialog/dialog";
import {Theme} from "../../../admin/permission/themes/theme";

declare var $;
@Component({
  selector: 'app-admin-dashboard',
  templateUrl: './admin-dashboard.component.html',
  styleUrls: ['./admin-dashboard.component.css']
})
export class AdminDashboardComponent implements OnInit {

  msg;
  themes:Array<Theme>;
  constructor(
    private pageSubject : PageSubject,
    private dialogService : OneDialogService
  ) { }

  ngOnInit() {
    this.pushPage();
    // this.dtTables();
    // this.bsTable();
  }
  private pushPage(){
    this.pageSubject.pushPage("仪表盘","控制面板",null);
  }





  open(type,isModal){
    let dialog = new Dialog();
    // dialog.type=Dialog.DialogType.Success;
    dialog.type= type;
    dialog.isModal = isModal;
    dialog.title = "成功信息";
    dialog.content="操作成功";
    dialog.cancelBtn="取消";
    dialog.okBtn ="提交";
    dialog.closeFun = ()=> {alert("关闭");};
    dialog.cancelBtnFun = ()=> {alert("取消");}
    dialog.okBtnFun = ()=> {alert("保存成功");};
    this.dialogService.push(dialog);
  }


  bsTable(){
    $("#bsTable").bootstrapTable({
      columns:[
        { field: "id", checkbox:true },
        { field: "accountName", title:'账户名' },
        { field: "themeId",title:'主题'},
        { field: "portalId",title:'门户' },
        { field: "isEnable" ,title:'是否启用'},
        { field: "isAudit",title:'是否审核' },
        { field: "isAllowPc",title:'是否允许PC登录'},
        { field: "isAllowApp",title:'是否允许APP登录'},
        { field: "creator",title:'创建人',sortable:true,order:'asc'},
        { field: "createDate" ,title:'创建日期',sortable:true,order:'desc'}
      ],
      sidePagination:'server',
      pagination:true,
      pageNumber:1,
      pageSize:10,
      pageList:[10, 25, 50, 100],
      url:"api/accounts/bspagelist?format=json",
      method:'post',
      queryParams:function(params){
        params.name = "1111";
        return params;
      },
      responseHandler:(result)=>{
        // let json = {};
        // json['total'] = result.recordsFiltered;
        // json['rows'] = result.data;
        // return json;
        return result;
      },
      sortable:true,
      order:'asc',
      search:true,
      searchOnEnterKey:true,
      searchText:'',
      showColumns:true,
      showRefresh:true,
      minimumCountColumns:3,
      cardView:false,
      smartDisplay:true//智能显示分页或card视图
    });
  }

  dtTables(){
    let dtTable = $("#dtTable").DataTable({
       "serverSide": true,
       "processing": true,
        "ajax": {
          "url":"api/accounts/pagelist?format=json",
          "type": "POST",
          // "contentType": "application/json",
          "data" : function(d){
            d.accountName=$("#accountName").val();
            d.isEnable = $("input[name=isEnable]:checked").val();
            d.pageData = JSON.stringify(d);
            //附加参数 d.key = value;
            console.log("pageData:" +JSON.stringify(d));
          }
          // "data" : function(d){
          //   return JSON.stringify( d );
          // }
        },
      "columns": [
        { "data": "id" },
        { "data": "accountName" ,"name":"ACCOUNT_NAME"},
        { "data": "themeId"},
        { "data": "portalId" },
        { "data": "isEnable" },
        { "data": "isAudit" },
        { "data": "isAllowPc"},
        { "data": "isAllowApp"},
        { "data": "creator"},
        { "data": "createDate" ,"name":"CREATE_DATE"}
      ],
      "columnDefs": [
        {
          "targets" : [ 0 ],
          "visible" : false//隐藏列
        },
        {
          "targets" : [ 1,2,3,4,5,6,7,8 ],
          "orderable" : false//不排序
        },
        {
          "targets" : [ 4,5,6,7 ],
          "render" : function(data, type, full, meta){
            if(data == "1"){
              return "是";
            }else{
              return "否";
            }
          }
        }
      ],
      "pagingType": "full_numbers",
      // "dom":"rtilp",
      "dom":"rt<'row'<'col-xs-2'i><'col-xs-2'l><'col-xs-8'p>>",
      // "dom":"<'row'<'col-xs-3'f>>rt<'row'<'col-xs-2'i><'col-xs-2'l><'col-xs-8'p>>",
      "lengthMenu": [ [10, 25, 50, 100], [10, 25, 50, 100] ],
      "language": {
        "lengthMenu": "每页 _MENU_ 条",
        "zeroRecords": "无数据",
        "info":"_START_ - _END_ 条/已筛选_TOTAL_ 条",
        "infoEmpty": "0 - 0 条/共 0 条",
        "infoFiltered": "(总共 _MAX_ 条)",
        "search":         "查询",
        "paginate": {
          "first":      "首页",
          "last":       "尾页",
          "next":       "下一页",
          "previous":   "上一页"
        },
        "processing":     "数据加载中..."
      },
      "search":true
      });
    $("#dtSearch").on("click",function(){
      dtTable.draw();
    });

  }
}
